function [sd_calib,sh_emp_q_target]=sd_sh_emp_q_calib(inputs_struc,sd_entrant)

grid_dev_sd=inputs_struc.grid_dev_sd;mean_entrant=inputs_struc. mean_entrant; 
prod_grid_size=inputs_struc.prod_grid_size; sigma = inputs_struc.sigma;
no_q = inputs_struc.no_q; size_q_target = inputs_struc.size_q_target;
size_hist_cutoffs = inputs_struc.size_hist_cutoffs; sh_emp_q_guess = inputs_struc.sh_emp_q_guess;
pdf_hist_pts_target = inputs_struc.pdf_hist_pts_target;

 % Grid for productivity and pdf given mean and std dev of productivity
    % distribution
    A_i_grid = exp(linspace(-grid_dev_sd*sd_entrant + mean_entrant,grid_dev_sd*sd_entrant + mean_entrant,prod_grid_size));
    A_q_i = repmat(A_i_grid,no_q,1);
    cdf_A_i = logncdf(A_i_grid,mean_entrant,sd_entrant);
    pdf_A_i = [0, diff(cdf_A_i)];
    pdf_A_i = pdf_A_i./(sum(pdf_A_i));
    pdf_A_q_i = repmat(pdf_A_i,no_q,1);

    % Given productivity distribution (dispersion) and avg size target of different quality levels,
    % can figure out exact size of each cell.
    l_q_i_prop = A_q_i.^(sigma-1);
    size_mult_factor = repmat(size_q_target,1,prod_grid_size)./repmat(sum(l_q_i_prop.*pdf_A_q_i,2),1,prod_grid_size);
    size_q_i = l_q_i_prop.*size_mult_factor;
    
    
    
    % Find the share of employment in each quality level (rows) that is in each
    % size category (column) of the histogram points.
    for ii = 1:size(size_hist_cutoffs,1)

        if ii == 1
            indicator = (size_q_i<=size_hist_cutoffs(ii));
        else
            indicator = (size_q_i>=size_hist_cutoffs(ii-1)&size_q_i<size_hist_cutoffs(ii));
        end
        sh_emp_hist_cats_q(:,ii) = sum(size_q_i.*pdf_A_q_i.*indicator,2)./sum(size_q_i.*pdf_A_q_i,2);

    end
    indicator = (size_q_i>size_hist_cutoffs(ii));
    sh_emp_hist_cats_q(:,ii+1) = sum(size_q_i.*pdf_A_q_i.*indicator,2)./sum(size_q_i.*pdf_A_q_i,2);
    
    handle_sd_min = @(sh_emp_q)sd_dev(sh_emp_q,sh_emp_hist_cats_q,pdf_hist_pts_target);
    solution_fmin = fmincon(handle_sd_min,sh_emp_q_guess,[],[],[],[],zeros(size(sh_emp_q_guess,1),1),ones(size(sh_emp_q_guess,1),1));
    
    sh_emp_q_target = [solution_fmin;1-sum(solution_fmin)];
    
    % Given targets for sh of emp in each q and avg size of each q, can figure
    % out the relative number of firms in each q and so also the pdf of firms
    % for each point on grid
    M_q_target = sh_emp_q_target./size_q_target;
    M_q_target_rel = M_q_target./M_q_target(1,1);
    model_firm_pdf = repmat(M_q_target_rel,1,prod_grid_size).*pdf_A_q_i;
    model_firm_pdf = model_firm_pdf./sum(sum(model_firm_pdf ));

    % sd of log employment
    avg_size_calib = sum(sum(log(size_q_i).*model_firm_pdf));
    sd_calib = sqrt(sum(sum((log(size_q_i).^2).*model_firm_pdf)) - avg_size_calib^2);
    
end
